package com.general.framework.mp;

import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
        // 创建一个 TreeMap
        TreeMap<Integer, String> treeMap = new TreeMap<>();

        // 插入元素
        treeMap.put(3, "Three");
        treeMap.put(1, "One");
        treeMap.put(2, "Two");

        // 遍历 TreeMap
        for (Integer key : treeMap.keySet()) {
            System.out.println(key + ": " + treeMap.get(key));
        }

        // 获取第一个和最后一个键
        System.out.println("First Key: " + treeMap.firstKey());
        System.out.println("Last Key: " + treeMap.lastKey());

        // 获取小于等于 2 的最大键
        System.out.println("Floor Key: " + treeMap.floorKey(2));

        // 获取大于等于 2 的最小键
        System.out.println("Ceiling Key: " + treeMap.ceilingKey(2));
    }
}
